//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Resource Application Block
//===============================================================================

using System.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder;

namespace EntLibContrib.Resource.Configuration
{
	/// <summary>
	/// Configuration data defining ResourceManagerData. Defines the information needed to properly configure
	/// a ResourceManager instance.
	/// </summary>    	
	public class ResourceManagerData : NamedConfigurationElement
	{
		#region Constants
		private const string resourceProviderNameProperty = "resourceProviderName";
		#endregion

		#region Properties
		/// <summary>
		/// ResourceProviderData object from configuration describing how data is stored 
		/// in the resource.
		/// </summary>
		[ConfigurationProperty(resourceProviderNameProperty, IsRequired = true)]
		public string ResourceProvider
		{
			get { return (string)base[resourceProviderNameProperty]; }
			set { base[resourceProviderNameProperty] = value; }
		}
		#endregion

		#region Construction
		/// <summary>
		/// Initialize a new instance of the <see cref="ResourceManagerData"/> class.
		/// </summary>
		public ResourceManagerData ()
		{
		}

		/// <summary>
		/// Initialize a new instance of the <see cref="ResourceManagerData"/> class.
		/// </summary>
		/// <param name="name">The name of the <see cref="ResourceManagerData"/>.</param>
		/// <param name="resourceProvider">
		/// ResourceProviderData object from configuration describing how data is stored
		/// in the resource.
		/// </param>
		public ResourceManagerData (string name, string resourceProvider) : base(name)
		{
			this.ResourceProvider = resourceProvider;
		}
		#endregion
	}

	internal class ResourceManagerDataRetriever : IConfigurationNameMapper
	{
		#region Public Methods
		/// <summary>
		/// This method supports the Enterprise Library infrastructure and is not intended to be used directly from your code.
		/// Returns the default resource manager name from the configuration in the <paramref name="configSource"/>, if the
		/// value for <paramref name="name"/> is <see langword="null"/> (<b>Nothing</b> in Visual Basic).
		/// </summary>
		/// <param name="name">The current name.</param>
		/// <param name="configurationSource">The source for configuration information.</param>
		/// <returns>The default resource manager name if <paramref name="name"/> is <see langword="null"/> (<b>Nothing</b> in Visual Basic),
		/// otherwise the original value for <b>name</b>.</returns>
		public string MapName(string name, IConfigurationSource configurationSource)
		{
			if ( name == null )
			{
				ResourceConfigurationView view = new ResourceConfigurationView(configurationSource);
				return view.DefaultResourceManager;
			}

			return name;
		}
		#endregion
	}
}